﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MinhTam.DataAccess.Web;
using MinhTam.Objects;
using System.Data;
using System.IO;
public partial class QuanLyAlbum : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
          
         
            InitListData();
        }
    }

    private void InitListData()
    {
        lblNoctice.Visible = false;
        string sql = string.Format(@"SELECT aa.ALBUM_ID,u.FULL_NAME  NGUOI_TAO,aa.NGAY_TAO, aa.tEN_ALBUM +' ('+ CONVERT(NVARCHAR(10), (SELECT COUNT(*) FROM HINH_ANH ha WHERE ha.Album_ID=aa.ALBUM_ID)) +N' ảnh )'  TEN_ALBUM,
        (SELECT TOP 1 '{0}'+ ha.URL_IMAGE 
        FROM HINH_ANH ha WHERE ha.Album_ID=aa.ALBUM_ID AND ha.IS_ANH_BIA='Y') THUMBNAIL
        FROM ALBUM_ANH aa
        LEFT JOIN USERS u ON u.USERS_ID=aa.NGUOI_TAO
        WHERE AA.ALBUM_ID NOT IN {1}", HelpConstEnum.UPLOAD_IMAGE_FOLDER, HelpConstEnum.FIX_ALBUM);
        DataTable dt = SQLConnectWeb.GetTable(sql);
        listImage.DataSource = dt;
        listImage.DataBind();
       
    }


    protected void btnDel_cLick(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        string sqlG = "select URL_IMAGE from hinh_anh where ALBUM_ID =" + id;
        DataTable dt = SQLConnectWeb.GetTable(sqlG);
        string sql = string.Format("delete from ALBUM_ANH where ALBUM_ID={0}", id);
        if (SQLConnectWeb.ExecSQL(sql))
        {
            InitListData();
            if (dt != null && dt.Rows.Count > 0)
            {

                foreach (DataRow r in dt.Rows)
                {
                    HelpUtil.DeleteFile(this,HelpConstEnum.UPLOAD_IMAGE_FOLDER + r[0].ToString());
                   
                }
            }
        }
        else
        {
            lblNoctice.Text = "* Xóa album ảnh không thành công!";
            lblNoctice.Visible = true;
        }
    }
}
